export default {
  name: 'edit',
  props: {
    id: {
      type: [String, Number],
      default: ''
    },
    idName: {
      type: String,
      default: 'id'
    },
    visible: {
      type: Boolean,
      default: false
    },
    readOnly: {
      type: Boolean,
      default: false
    },
    resetTitle: {
      type: Boolean,
      default: true
    },
    isDetail: {
      type: Boolean,
      default: false
    },
    defaultTitle: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      temp: {

      },
      useTemp: true,
      reloadList: true,
      formId: 'dataForm',
      title: '',
      requiredSaveConfirm: false,
      saveName: 'save',
      detailName: 'detail',
      existsName: 'exists',
      requiredClose: true,
      saveArgName: 'temp',
      showAllProps: false,
      loading: false,
      tempVisible: this.visible,
      saveConfirmMsg: '确定要保存吗？',
      rules: {}
    }
  },
  methods: {
    detail() {
      const detail = this.api[this.detailName]
      this.loading = true
      detail(this.id).then(data => {
        this.loading = false
        if (this.useTemp) {
          if (data instanceof Array) {
            data = {
              list: data
            }
          }
          if (this.detailDataProcessor) {
            data = this.detailDataProcessor(data)
          }
          if (this.showAllProps) {
            this.temp = data
          } else {
            const temp = { ...this.temp }
            Object.keys(temp).forEach(key => {
              temp[key] = data[key]
            })
            this.temp = temp
          }
          if (this.detailCallback) {
            this.detailCallback(data)
          }
        } else {
          this.detailCallback(data)
        }
      }).catch(e => {
        this.loading = false
      })
    },
    resetTemp() {
      const form = this.$refs[this.formId]
      if (!form) {
        return
      }
      form.resetFields()
    },
    validateField(fieldName) {
      this.$refs[this.formId].validateField(fieldName)
    },
    clearValidate() {
      const form = this.$refs[this.formId]
      if (!form) {
        return
      }
      form.clearValidate()
    },
    handlerClose() {
      this.resetTemp()
      this.$emit('closed')
    },
    loadList() {
      if (this.reloadList) {
        const list = this.getParentMethod(this, 'getList')
        if (!list) {
          return
        }
        list()
      }
    },
    submit() {
      if (this.beforeSubmit) {
        const result = this.beforeSubmit()
        if (result === false) {
          return
        }
      }
      if (this.requiredSaveConfirm) {
        const warning = this.saveConfirmMsg
        this.$confirm(
          warning,
          '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(() => {
            this.doSave()
          }).catch(e => {

          })
        return
      }
      this.doSave()
    },
    doSave() {
      this.$refs[this.formId].validate(valid => {
        if (valid) {
          this.loading = true
          const save = this.api[this.saveName]
          let data = this[this.saveArgName]
          if (this.dataProcessor) {
            data = this.dataProcessor(data)
          }
          save(data).then(() => {
            this.tempVisible = false
            if (this.requiredClose) {
              this.handlerClose()
            }
            this.loadList()
            if (this.afterSubmit) {
              this.afterSubmit()
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    getParentMethod(el, methodName) {
      if (!el.$parent) {
        return null
      }
      if (el.$parent[methodName]) {
        return el.$parent[methodName]
      }
      return this.getParentMethod(el.$parent, methodName)
    },
    async exists(data) {
      const exists = this.api[this.existsName]
      return await exists(data)
    }
  },
  watch: {
    id(val) {
      this.temp[this.idName] = val
      this.$emit('idChange', val, this.visible, this)
      if (!val) {
        return
      }
      if (this.visible) {
        return
      }
      this.detail()
    },
    visible(val) {
      this.tempVisible = val
      this.$emit('visibleChange', val, this.id, this)
      if (this.resetTitle) {
        this.title = this.id ? '编辑' : '新增'
      } else {
        this.title = this.defaultTitle
      }
      if (!val) {
        return
      }
      if (!this.id) {
        return
      }
      this.detail()
    }
  }
}
